Page({
  data: {
    theme: 'light',
    schema: [
      {
        label: 'key',
        prop: 'key',
        type: 'text'
      }, {
        label: 'data',
        prop: 'data',
        type: 'text'
      }
    ],
    title: '数据缓存',
    formData: {
      key: '',
      data: ''
    },
    result: ''
  },

  formDataChange(e) {
    this.setData({
      formData: e.detail.formData
    })
  },

  getStorage() {
    const { key } = this.data.formData
    if (key) {
      wx.getStorage({
        key,
        complete: (e) => {
          this.setData({
            result: e
          })
        }
      })
    } else {
      wx.getStorageInfo({
        complete: (e) => {
          this.setData({
            result: e
          })
        }
      })
    }
  },
  getStorage1() {
    const { key, data } = this.data
    let storageData

    if (key.length === 0) {
      this.setData({
        key,
        data,
      })
      wx.showModal({
        title: '读取数据失败',
        content: 'key 不能为空'
      })
    } else {
      storageData = wx.getStorageSync(key)
      console.log(storageData)
      if (storageData === '') {
        this.setData({
          key,
          data: storageData
        })
        wx.showModal({
          title: '读取数据失败',
          content: '找不到 key 对应的数据'
        })
      } else {
        this.setData({
          key,
          data: storageData
        })
        wx.showModal({
          title: '读取数据成功',
          content: storageData,
        })
      }
    }
  },

  setStorage() {
    const { key, data } = this.data.formData
    if (key.length === 0) {
      wx.showModal({
        title: '保存数据失败',
        content: 'key 不能为空'
      })
    } else {
      wx.setStorage({
        key,
        data,
      })
      wx.showToast({
        title: '存储数据成功',
      })
    }
  },

  clearStorage() {
    wx.clearStorage({
      complete: (e) => {
        this.setData({
          result: e
        })
      }
    })
  },

  // 同步存储数据
  setStorageSync() {
    const { key, data } = this.data.formData
    if (key.length === 0) {
      wx.showModal({
        title: '保存数据失败',
        content: 'key 不能为空'
      })
    } else {
      try {
        wx.setStorageSync(key, data)
        this.setData({
          'formData.key': key,
          'formData.data': data,
          result: '同步存储成功'
        })
      } catch (err) {
        wx.showModal({
          title: '保存数据失败',
          content: err.errMsg
        })
      }
    }
  },

  // 同步获取数据
  getStorageSync() {
    const { key } = this.data.formData
    if (key.length === 0) {
      wx.showModal({
        title: '读取数据失败',
        content: '没有数据'
      })
    } else {
      try {
        const data = wx.getStorageSync(key)
        console.log("data",data)
        this.setData({
          'formData.data': data,
          result: '同步获取成功: ' + data
        })
      } catch (err) {
        wx.showModal({
          title: '读取数据失败',
          content: err.errMsg
        })
      }
    }
  },

  // 移除指定key的数据
  removeStorage() {
    const { key } = this.data.formData

    wx.removeStorage({
      key,

    })

  },

  // 同步移除指定key的数据
  removeStorageSync() {
    const { key } = this.data.formData
    try {
      wx.removeStorageSync(key)
      this.setData({
        result: '同步移除成功'
      })
      wx.showToast({
        title: '移除数据成功',
      })
    } catch (err) {
      wx.showModal({
        title: '移除数据失败',
        content: err.errMsg
      })
    }

  },

  // 获取存储信息
  getStorageInfoSync() {
    try {
      const info = wx.getStorageInfoSync()
      this.setData({
        result: `存储信息: keys=${info.keys.length}, currentSize=${info.currentSize}KB, limitSize=${info.limitSize}KB`
      })
      wx.showModal({
        title: '存储信息',
        content: `键数量: ${info.keys.length}\n当前大小: ${info.currentSize}KB\n限制大小: ${info.limitSize}KB`,
      })
    } catch (err) {
      wx.showModal({
        title: '获取存储信息失败',
        content: err.errMsg
      })
    }
  },

  // 批量存储
  batchSetStorage() {
    const testData = [
      { key: 'test1', value: 'value1' },
      { key: 'test2', value: 'value2' },
      { key: 'test3', value: 'value3' }
    ]

    wx.batchSetStorage({
      kvList: testData,
      success: (e) => {
        this.setData({
          result: e
        })
      }
    })
  },

  // 同步批量存储
  batchSetStorageSync() {
    const testData = [
      { key: 'sync_test1', value: 'sync_value1' },
      { key: 'sync_test2', value: 'sync_value2' },
      { key: 'sync_test3', value: 'sync_value3' }
    ]

    try {
      wx.batchSetStorageSync(testData)
      this.setData({
        result: '同步批量存储成功'
      })
    } catch (err) {
      wx.showModal({
        title: '批量存储失败',
        content: err.errMsg
      })
    }
  },

  // 批量获取
  batchGetStorage() {
    const keys = ['test1', 'test2', 'test3']

    wx.batchGetStorage({
      keyList: keys,
      success: (e) => {
        this.setData({
          result: e
        })
      }
    })
  },

  // 同步批量获取
  batchGetStorageSync() {
    const keys = ['sync_test1', 'sync_test2', 'sync_test3']

    try {
      const result = wx.batchGetStorageSync(keys)
      this.setData({
        result: result
      })

    } catch (err) {
      wx.showModal({
        title: '批量获取失败',
        content: err.errMsg
      })
    }
  },
  // 创建Buffer URL
  createBufferURL() {
    
    const testData = 'Hello, Buffer URL!'
    // 创建ArrayBuffer
    const buffer = new ArrayBuffer(testData.length)
    const view = new Uint8Array(buffer)
    for (let i = 0; i < testData.length; i++) {
      view[i] = testData.charCodeAt(i)
    }
    
    try {   
      
      const url = wx.createBufferURL(buffer)
      this.setData({
        result: `Buffer URL创建成功!\nURL: ${url}`
      })
    } catch (err) {
      this.setData({
        result: `Buffer URL创建失败!\nURL: ${err}`
      })
    }
  },

  // 销毁Buffer URL
  revokeBufferURL() {
    
    const result = this.data.result
    
    // 从结果中提取URL（简单提取，实际可能需要更复杂的解析）
    let url = ''
    if (result && result.includes('URL:')) {
      const urlMatch = result.match(/URL:\s*([^\n]+)/)
      if (urlMatch) {
        url = urlMatch[1].trim()
      }
    }
    
    
    if (!url) {
      wx.showModal({
        title: '销毁失败',
        content: '没有找到可销毁的URL，请先创建Buffer URL'
      })
      return
    }

    try {
      // wx.revokeBufferURL 是同步调用
      wx.revokeBufferURL(url)
      
      this.setData({
        result: 'Buffer URL已销毁'
      })
    } catch (err) {
      this.setData({
        result: `销毁Buffer URL失败!\nURL: ${err}`
      })
    }
  },

  // 同步清空存储
  clearStorageSync() {
    try {
      wx.clearStorageSync()
      this.setData({
        result: '同步清空存储成功'
      })
      wx.showToast({
        title: '清空存储成功',
      })
    } catch (err) {
      wx.showModal({
        title: '清空存储失败',
        content: err.errMsg
      })
    }
  },
  onLoad() {
    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({ theme }) => {
        this.setData({ theme })
      })
    }
  }
})
